package com.chenyi.algorithm.sort;

import java.util.Arrays;

/**
 * @author chenyi
 */
public class InsertSorting {
    public static void main(String[] args) {

        int[] arr =  {8, 1, 22, 5, 123, 3};

        sort(arr);

        System.out.println(Arrays.toString(arr));


    }

    public static int[] sort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int insertVal = arr[i];
            int insertIndex = i - 1;

            while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }

            arr[insertIndex + 1] = insertVal;
        }
        return arr;
    }
}
